home *** CD-ROM | disk | FTP | other *** search
- <?php
- //************************************************************************
- //************************************************************************
- //
- // Creata da Rosario Sensale
- // E.Mail: rs_linux@email.it
- // Date: 2004/03/01
- // Ver.: 1.00.00
- // propietα:
- //
- // * SetLastName (Specifica il Cognome)
- // * SetName (Specifica il nome)
- // * SetCom (Specifica il Comune)
- // * SetBornDate (Specifica la data di nascita)
- // * SetSex (Specifica il Sesso)
- //
- // Metodi:
- // * GetLastName (Restituisce il Cognome)
- // * GetName (Restituisce il nome)
- // * GetCom (Restituisce il Comune)
- // * GetBornDate (Restituisce la data di nascita)
- // * GetSex (Restituisce il Sesso)
- // * Create_CF (Crea il codice fiscale)
- // * Check_CF (Controlla il codice fiscale
- // (restituisce 1 se Φ corretto)
- //
- // Ritorno della funzione Create_CF:
- // In caso di errore la funzione ritorna:
- // -1 -> Cognome non presente
- // -2 -> Nome non presente
- // -3 -> Data di nascita non presente
- // -4 -> Sesso non presente
- // -5 -> Sesso non valido (diverso da M,F);
- // -6 -> Comune non specificato
- // -7 -> Comune non presente in archivio
- // -8 -> Errore nella data
- // -9 -> I codifi fiscali sono diversi
- //************************************************************************
- //************************************************************************
-
- class Class_CF{
- var $cognome;
- var $nome;
- var $sesso;
- var $data_nascita;
- var $comune;
-
- //Class construct
- function __construct(){
- $this->comune='';
- $this->data_nascita='';
- $this->sesso='';
- $this->nome='';
- $this->cognome='';
- $this->codice_comune='';
- }
-
- //Class destruct
- function __destruct(){
- }
-
- //Set il cognome
- function SetLastName($object){
- $this->cognome=$object;
- }
-
- //Set il nome
- function SetName($object){
- $this->nome=$object;
- }
-
- //Setta la data di nascita
- function SetBornDate($object){
- $this->data_nascita=$object;
- }
-
- //Setta il sesso
- function SetSex($object){
- $this->sesso=$object;
- }
-
- //Setta il comune
- function SetCom($object){
- $this->comune=$object;
- }
-
-
- //Restituisce il cognome
- function GetLastName(){
- return $this->cognome;
- }
-
- //Restituisce il nome
- function GetName(){
- return $this->nome;
- }
-
- //Restituisce la data di nascita
- function GetBornDate(){
- return $this->data_nascita;
- }
-
- //Restituisce il comune
- function GetCom(){
- return $this->comune;
- }
-
- //Restituisce il sesso
- function GetSex(){
- return $this->sesso;
- }
-
-
- //funzione utile x controllare il sesso
- function CheckSesso(&$Sesso){
- $Sesso=strtoupper($Sesso);
-
- if (($Sesso != 'M') && ($Sesso != 'F')){
- return 0;
- }elseif (strLen($Sesso) > 1){
- return 0;
- }else{
- return 1;
- }
- }
-
- //Restituisce il codice del comune
- function Comune($Comune){
- $codice_comune='0';
- $fp = fopen("Comuni.csv","r");
- while ( ($stringa = fgets($fp,4096)) && ($codice_comune=="0") ){
- $campi=explode(";",$stringa);
- $campi[2]=trim($campi[2]);
- if (strCmp($campi[2],$Comune) == 0){
- $codice_comune=$campi[0];
- }
- }
- fclose($fp);
- return $codice_comune;
- }
-
-
- // Data una stringa restituisce quest'ultima senza
- //le vocali CASA -> CS
- function OnlyCons($strStringa) {
- $Max=strLen($strStringa);
- $Temp='';
- $Char='';
-
- if ($Max > 0){
- for ($i=0;$i<$Max;$i++){
- $Char=substr($strStringa,$i,1);
- if (($Char != ' ') && ($Char != 'A') && ($Char != 'E') && ($Char != 'I') && ($Char != 'O') && ($Char != 'U')){
- $Temp .= $Char;
- }
- }
- return trim($Temp);
- }else{
- return '';
- }
- }
-
- //Data una stringa restituisce quest'ultima senza
- //le consolanti CASA -> AA
- function OnlyVoc($strStringa) {
- $Max=strLen($strStringa);
- $Temp='';
- $Char='';
-
- if ($Max > 0){
- for ($i=0;$i<$Max;$i++){
- $Char=substr($strStringa,$i,1);
- if ((($Char == 'A') || ($Char == 'E') || ($Char == 'I') || ($Char == 'O') || ($Char == 'U')) && ($Char != ' ')){
- $Temp .= $Char;
- }
- }
- return $Temp;
- }else{
- return '';
- }
- }
-
- //Elimina tutte le lettere accentate
- //Esempio MACCARO' -> MACCARO
- function NoAccento($strStringa){
- $Accentate="└╚╔╠╥┘αΦΘ∞≥∙";
- $No_Accentate="AEEIOUAEEIOU";
- $Max=strLen($strStringa);
- if ($Max > 0){
- for ($i=0;$i<$Max;$i++){
- $pos=strpos($Accentate,substr($strStringa,$i,1));
- if ($pos){
- $strStringa[$i]=substr($No_Accentate,$pos,1);
- }
- }
- }
- return $strStringa;
- }
-
- //Restituisce il Cognome nella forma corretta
- function GetLastname($strStringa){
- $Consol=$this->OnlyCons($strStringa);
- $Max=strLen($Consol);
-
- if ($Max > 3) {
- return substr($Consol,0,3);
- }elseif ($Max < 3){
- $Vocali=OnlyVoc($strStringa);
- return $Consol . substr($Vocali,0,abs(3-$Max));
- }elseif ($Max == 3){
- return $Consol;
- }
- }
-
- //Restituisce il nome nella forma corretta
- function GetName($strStringa){
- $Consol=$this->OnlyCons($strStringa);
- $Max=strLen($Consol);
-
- if ($Max > 3) {
- return $Consol[0] . $Consol[2] . $Consol[3];
- }elseif ($Max < 3){
- $Vocali=OnlyVoc($strStringa);
- return $Consol . substr($Vocali,0,abs(3-$Max));
- }elseif ($Max == 3){
- return $Consol;
- }
- }
-
-
- //controllo del giorno
- function CreateDay($giorno,$Sesso){
- if ($Sesso == 'M'){
- if (strlen($giorno)<2){$giorno= "0" . $giorno;} //4 -> 04
- }elseif ($Sesso == 'F'){
- $giorno=intval($giorno)+40;
- }
- return $giorno;
- }
-
-
- //controllo della carattere di controllo
- function CheckControlKey($codice_fiscale){
- //A
- $Matrice_Controllo[65][0] = 1;
- $Matrice_Controllo[65][1] = 0;
- // "B"
- $Matrice_Controllo[66][0] = 0;
- $Matrice_Controllo[66][1] = 1;
- // "C"
- $Matrice_Controllo[67][0] = 5;
- $Matrice_Controllo[67][1] = 2;
- // "D"
- $Matrice_Controllo[68][0] = 7;
- $Matrice_Controllo[68][1] = 3;
- // "E"
- $Matrice_Controllo[69][0] = 9;
- $Matrice_Controllo[69][1] = 4;
- // "F"
- $Matrice_Controllo[70][0] = 13;
- $Matrice_Controllo[70][1] = 5;
- // "G"
- $Matrice_Controllo[71][0] = 15;
- $Matrice_Controllo[71][1] = 6;
- // "H"
- $Matrice_Controllo[72][0] = 17;
- $Matrice_Controllo[72][1] = 7;
- // "I"
- $Matrice_Controllo[73][0] = 19;
- $Matrice_Controllo[73][1] = 8;
- //J"
- $Matrice_Controllo[74][0] = 21;
- $Matrice_Controllo[74][1] = 9;
- // "K"
- $Matrice_Controllo[75][0] = 2;
- $Matrice_Controllo[75][1] = 10;
- // "L"
- $Matrice_Controllo[76][0] = 4;
- $Matrice_Controllo[76][1] = 11;
- // "M"
- $Matrice_Controllo[77][0] = 18;
- $Matrice_Controllo[77][1] = 12;
- // "N"
- $Matrice_Controllo[78][0] = 20;
- $Matrice_Controllo[78][1] = 13;
- // "O"
- $Matrice_Controllo[79][0] = 11;
- $Matrice_Controllo[79][1] = 14;
- // "P"
- $Matrice_Controllo[80][0] = 3;
- $Matrice_Controllo[80][1] = 15;
- // "Q"
- $Matrice_Controllo[81][0] = 6;
- $Matrice_Controllo[81][1] = 16;
- // "R"
- $Matrice_Controllo[82][0] = 8;
- $Matrice_Controllo[82][1] = 17;
- // "S"
- $Matrice_Controllo[83][0] = 12;
- $Matrice_Controllo[83][1] = 18;
- // "T"
- $Matrice_Controllo[84][0] = 14;
- $Matrice_Controllo[84][1] = 19;
- // "U"
- $Matrice_Controllo[85][0] = 16;
- $Matrice_Controllo[85][1] = 20;
- // "V"
- $Matrice_Controllo[86][0] = 10;
- $Matrice_Controllo[86][1] = 21;
- // "W"
- $Matrice_Controllo[87][0] = 22;
- $Matrice_Controllo[87][1] = 22;
- // "X"
- $Matrice_Controllo[88][0] = 25;
- $Matrice_Controllo[88][1] = 23;
- // "Y"
- $Matrice_Controllo[89][0] = 24;
- $Matrice_Controllo[89][1] = 24;
- // "Z"
- $Matrice_Controllo[90][0] = 23;
- $Matrice_Controllo[90][1] = 25;
- // "0"
- $Matrice_Controllo[48][0] = 1;
- $Matrice_Controllo[48][1] = 0;
- // "1"
- $Matrice_Controllo[49][0] = 0;
- $Matrice_Controllo[49][1] = 1;
- // "2"
- $Matrice_Controllo[50][0] = 5;
- $Matrice_Controllo[50][1] = 2;
- // "3"
- $Matrice_Controllo[51][0] = 7;
- $Matrice_Controllo[51][1] = 3;
- // "4"
- $Matrice_Controllo[52][0] = 9;
- $Matrice_Controllo[52][1] = 4;
- // "5"
- $Matrice_Controllo[53][0] = 13;
- $Matrice_Controllo[53][1] = 5;
- // "6"
- $Matrice_Controllo[54][0] = 15;
- $Matrice_Controllo[54][1] = 6;
- // "7"
- $Matrice_Controllo[55][0] = 17;
- $Matrice_Controllo[55][1] = 7;
- // "8"
- $Matrice_Controllo[56][0] = 19;
- $Matrice_Controllo[56][1] = 8;
- // "9"
- $Matrice_Controllo[57][0] = 21;
- $Matrice_Controllo[57][1] = 9;
-
- //calcola carattere di controllo
- $codcontrollo = 0;
- //scorre $codice fino ad ora calcolato
- For ($i= 0;$i<strLen($codice_fiscale);$i++){
- $codcontrollo += $Matrice_Controllo[ord(substr($codice_fiscale, $i, 1))][$i % 2];
- }
- return chr(65 + ($codcontrollo % 26));
-
- }
-
- //controlla la data
- //restituisce TRUE se la data Φ esatta
- function CheckData($giorno,$mese,$anno){
- if (checkdate(intval($mese),intval($giorno),intval($anno)) == true){
- return true;
- }else{
- return false;
- }
- }
-
-
- //Crea il codice fiscale
- function Create_CF(){
- if (!isset($this->cognome)){
- return -1;
- }elseif (!isset($this->nome)){
- return -2;
- }elseif (!isset($this->data_nascita)){
- return -3;
- }elseif (!isset($this->sesso)){
- return -4;
- }elseif ($this->CheckSesso($this->sesso) == 0){
- return -5;
- }elseif (!isset($this->comune)){
- return -6;
- }else{
- $Mesi = "ABCDEHLMPRST"; //lettere x corrispondenza mesi
-
- //prendo il codice del comune
- $this->comune=strtoupper($this->comune);
- $this->codice_comune=$this->Comune($this->comune);
- $this->cognome=strtoupper($this->cognome);
- $this->nome=strtoupper($this->nome);
-
- if ($this->codice_comune == '0'){
- return -7;
- }else{ //si puo' proseguire
- //elimino le accentate
-
-
- $this->cognome=$this->NoAccento($this->cognome);
- $this->nome=$this->NoAccento($this->nome);
- $codice_fiscale = $this->GetLastname($this->cognome);
- $codice_fiscale .= $this->GetName($this->nome);
-
- //controllo la data
- $data=explode("/",$this->data_nascita);
- if (sizeof($data)!=3){
- return -8;
- }elseif ($this->CheckData($data[0],$data[1],$data[2]) == false) {
- return -8;
- } else {
- $anno=$data[2];
- $mese=$data[1];
- $giorno=$data[0];
- if (strlen($anno)<2){$anno= "0" . $anno;} //4 -> 04
- if (strlen($anno)==4){$anno=substr($anno,2,2);} //2004 -> 04
-
- $codice_fiscale .= $anno; //Aggiunta dell'anno
- $codice_fiscale .= substr($Mesi,($mese-1),1); //Mese
- }
- //codifica del giorno di nascita
- $codice_fiscale .= $this->CreateDay($giorno,$this->sesso);
- $codice_fiscale .= $this->codice_comune;
- $codice_fiscale .= $this->CheckControlKey($codice_fiscale);
-
- return $codice_fiscale;
- }
- }
- }
-
-
- //Controlla che il codice fiscale passato come
- //argomento Φ corretto
- //restituisce 1 se Φ corretto
- function Check_CF($cf){
- $codice_fiscale=$this->Create_CF();
- if (strCmp($cf,$codice_fiscale) != 0){
- return -9;
- }else{
- return 1;
- }
- }
-
- }
- ?>
-
-